/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.action;

import java.util.*;
import javax.swing.*;
import ch.trackedbean.binding.action.ActionManager.IActionHolder;
import ch.trackedbean.binding.action.MenuPath.MenuPathSegment;

/**
 * Helper class creating a selection dependent {@link JPopupMenu} using local actions and actions registered by the {@link ActionManager}.
 * 
 * @author M. Hautle
 */
public class LocalPopupManager extends MenuManager {
    /** Path representing the default group. */
    private static final MenuPath DEFAULT_GROUP = new MenuPath(new MenuPathSegment[0], null, Integer.MAX_VALUE);

    /** The local actions. */
    private final List<ActionEntry<ISelectionAction>> localActions = new ArrayList<ActionEntry<ISelectionAction>>();

    /**
     * Flag indicating if the popup should be populated using {@link ActionManager#addActions(List, IActionHolder)} and the appending the {@link #localActions}
     * (true) or just by appending the {@link #localActions} (false).
     */
    private boolean global = true;

    /** The menu. */
    private final JPopupMenu menu = new JPopupMenu();

    /**
     * Returns menu with the local actions and appends the one registered by the {@link ActionManager} if {@link #global} is set.
     * 
     * @param <T> The selection type
     * 
     * @param selection The selection for which the menu gets populated
     * @return The populated menu
     */
    public <T> JPopupMenu getMenu(List<T> selection) {
        return populate(selection, global);
    }

    /**
     * Returns the menu with the actions registered in the {@link ActionManager} and adds the local actions.
     * 
     * @param <T> The selection type
     * 
     * @param selection The selection for which the menu gets populated
     * @param global Flag indicating if the popup should be populated using {@link ActionManager#addActions(List, IActionHolder)} and the appending the
     *            {@link #localActions} (true) or just by appending the {@link #localActions} (false)
     * @return The populated menu
     */
    @SuppressWarnings("unchecked")
    public <T> JPopupMenu populate(List<T> selection, boolean global) {
        if (global)
            ActionManager.addActions(selection, this);
        for (ActionEntry<ISelectionAction> e : localActions)
            if (e.getAction().selectionChanged(selection))
                addAction(e);
        populate(menu);
        clear();
        return menu;
    }

    /**
     * Adds a local action.
     * 
     * @param <T> The selection type
     * 
     * @param action The action
     */
    public <T> void addLocalAction(ISelectionAction<T> action) {
        addLocalAction(DEFAULT_GROUP, Integer.MAX_VALUE, action);
    }

    /**
     * Adds a local action.
     * 
     * @param <T> The selection type
     * @param order The order value of the action (used to sort the menu, lower value come first)
     * @param action The action
     */
    public <T> void addLocalAction(int order, ISelectionAction<T> action) {
        addLocalAction(DEFAULT_GROUP, order, action);
    }

    /**
     * Adds a local action.
     * 
     * @param <T> The selection type
     * 
     * @param path The location where to add the action
     * @param action The action
     */
    public <T> void addLocalAction(MenuPath path, ISelectionAction<T> action) {
        addLocalAction(path, Integer.MAX_VALUE, action);
    }

    /**
     * Adds a local action.
     * 
     * @param <T> The selection type
     * 
     * @param path The location where to add the action
     * @param order The order value of the action (used to sort the menu, lower value come first)
     * @param action The action
     */
    public <T> void addLocalAction(MenuPath path, int order, ISelectionAction<T> action) {
        localActions.add(new ActionEntry<ISelectionAction>(path, order, action));
    }

    /**
     * Removes the given local action.
     * 
     * @param action The action to remove
     * @return True if the action was removed
     */
    public boolean removeLocalAction(ISelectionAction action) {
        for (int i = 0, cnt = localActions.size(); i < cnt; i++) {
            if (localActions.get(i).getAction() != action)
                continue;
            localActions.remove(i);
            return true;
        }
        return false;
    }

    /**
     * See {@link #global}.
     * 
     * @return Returns the global flag.
     */
    public boolean isGlobal() {
        return global;
    }

    /**
     * This settings affects the behavior of {@link #getMenu(List)}. See {@link #global}.<br>
     * 
     * @param global True if local and {@link ActionManager} actions should be added, false if only the local ones should be added
     */
    public void setGlobal(boolean global) {
        this.global = global;
    }
}
